package com.yayyay.jwt.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.yayyay.commom.entity.SysUser;
import com.yayyay.commom.utils.ResultBean;
import com.yayyay.jwt.model.dto.LoginDTO;
import com.yayyay.jwt.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author: liangSY
 * @Date: 2021/9/7 11:43
 * @ClassName: loginController
 */
@RestController
public class LoginController {


    @Resource
    UserService userService;

    @PostMapping("login")
    public ResultBean<SaTokenInfo> login(@RequestBody @Valid LoginDTO dto) {
        SysUser user = userService.findByAccount(dto.getAccount());
        StpUtil.login(user.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return ResultBean.success(tokenInfo);
    }

    @GetMapping("logout")
    public ResultBean logout() {
        StpUtil.logout();
        return ResultBean.success("登出成功");
    }
}
